var downTimes;

$().ready(function() {
	// 倒数
	var cnt = $('span[id^=long_]').length;
	downTimes = new Array(cnt);
	for(var i = 0; i < cnt; i++) {
		downTimes[i] = ($('#long_' + i).text() / 1000) - $("#systemTime").text();
	}
	setInterval(countDown, 1000);
	
	// TAB
	$('a[id^=tab_]').mouseenter(function() {
		$('div[id^=board_]').hide();
		$('a[id^=tab_]').attr('class', 'weekday');
		var no = this.id.replace('tab_', '');
		$(this).attr('class', 'weekday-fo');
		$('#board_' + no).show();
		$('#board_' + no + ' > a > img').each(function() {
			if ($(this).attr('src') != $(this).attr('original')) {
				$(this).attr('src', $(this).attr('original'));
			}
		});
	});
});

function countDown() {
	for(var i = 0; i < downTimes.length; i++) {
		var downTime = downTimes[i];
		if( downTime >= 0 ) {
			var showText;
			var mod = downTime % (24 * 60 * 60);
			var d = (downTime - mod) / (24 * 60 * 60);
			showText = d + ' 天 ';
			
			var mod2 = mod % (60 * 60);
			var h = (mod - mod2) / (60 * 60);
			showText = showText + add0(h) + ":";
			
			var mod3 = mod2 % 60;
			var m = (mod2 - mod3) / 60;
			showText = showText + add0(m) + ":" + add0(mod3);
			
			$('#show_' + i).text(showText);
		} else if(downTime == -5) {
			window.location.reload();
		}
		if(downTime > -5) {
			downTime--;
		}
		downTimes[i] = downTime;
	}
}

function add0(num){
	if(num<10)
		return "0"+num;
	return num;
}
